import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '@STmailboxesWeb/store/index'
// import VueCookies from 'vue-cookies'
import configs from '@/configs'


//新首页
// import newHome from '../components/page/newHome/route'

Vue.use(VueRouter)
const routes = [
  // { path: '/', redirect: { path: '/index' } },
  // 门户网站总路由
  {
    path: '/',
    component: () => import('@STmailboxesWeb/layout/Layout.vue'),
    redirect: { path: '/index' },
    name: '',
    children: [
      {
        path: '/index',
        component: () => import('@STmailboxesWeb/views/gateway/homePage/index.vue'),
        name: '信箱首页布局',
        meta: {
          title: '首页',
        },
      },
      {
        path: '/secretary',
        component: () => import('@STmailboxesWeb/views/gateway/secretary/index.vue'),
        name: '书记信箱',
        meta: {
          title: '书记信箱',
        },
      },
      {
        path: '/county',
        component: () => import('@STmailboxesWeb/views/gateway/county/index.vue'),
        name: '县长信箱',
        meta: {
          title: '县长信箱',
        },
      },
      {
        path: '/part',
        component: () => import('@STmailboxesWeb/views/gateway/part/index.vue'),
        name: '部门信箱',
        meta: {
          title: '部门信箱',
        },
      },
      {
        path: '/township',
        component: () => import('@STmailboxesWeb/views/gateway/township/index.vue'),
        name: '镇乡信箱',
        meta: {
          title: '镇乡信箱',
        },
      },

      // 子路由
      {
        path: '/notice',
        component: () => import('@STmailboxesWeb/views/gateway/childrens/notice.vue'),
        name: 'notice',
        meta: {
          title: '写信须知',
        }
      },
      {
        path: '/writing',
        component: () => import('@STmailboxesWeb/views/gateway/childrens/writing.vue'),
        name: 'writing',
        meta: {
          title: '写信中',
        }
      },

      {
        path: '/Board',
        component: () => import('@STmailboxesWeb/views/gateway/childrens/board.vue'),
        name: 'Board',
        meta: {
          title: '公告栏',
        }
      },
      {
        path: '/InfoSel',
        component: () => import('@STmailboxesWeb/views/gateway/childrens/InfoSel.vue'),
        name: '信息选登',
        meta: {
          title: '信息选登',
        }
      },
      {
        path: '/InfoDetail',
        component: () => import('@STmailboxesWeb/views/gateway/childrens/InfoDetail.vue'),
        name: 'InfoDetail',
        meta: {
          title: '信息详情',
        }
      },
      {
        path: '/infoReply',
        component: () => import('@STmailboxesWeb/views/gateway/childrens/infoReply.vue'),
        name: '信件回复',
        meta: {
          title: '信件回复',
        }
      },
      {
        path: '/infoQuery',
        component: () => import('@STmailboxesWeb/views/gateway/childrens/infoQuery.vue'),
        name: '信件查询',
        meta: {
          title: '信件查询',
        }
      }

    ]
  },
  // 管理系统总路由
  {
    path: '/Manage',
    component: () => import('@STmailboxesWeb/layout/LayoutManage.vue'),
    // redirect: { path: '/index' },
    name: '',
  },
  // 404界面（必须放置在路由最底部）
  {
    path: '*',
    component: () => import(/* webpackChunkName: "notFound" */ '@/components/notFound.vue')
  }
]

const router = new VueRouter({
  mode: 'history',
  base: configs.BASE_URL,
  // base: '/',
  routes
})
// 页面跳转回到最顶部
router.afterEach((to, from, next) => {
  window.scrollTo(0, 0)
})
let beforeRouter = []
let activeTab = '/index'
let allTab = ['/secretary', '/county', '/part', '/township']

router.beforeEach((to, from, next) => {
  const whiteList = ['/login', '/oalogin'] //路由白名单
  const token = sessionStorage.getItem('authorization_Ygd')
  // console.log("来吧姐妹。这里设置面包屑数据", to, from);
  // if (!whiteList.includes(to.path) && !token) {
  //   next({ path: "/login" });
  // } else {
  //   next();
  // }
  if (allTab.includes(to.path)) {
    activeTab = to.path//设置选中的tab选项卡
    // 但是我一旦回到allTab中的路由之后beforeRouter这里面的数据清空，只有一个当前路由
    beforeRouter = [{
      path: to.path,
      name: to.meta.title
    }]
  } else {
    // 如果这个路径之前没有出现过
    let some = beforeRouter.some((v) => {
      return v.path == to.path
    })
    if (!some) {
      beforeRouter.push({
        path: to.path,
        name: to.meta.title
      })
      // console.log('如果这个路径之前没有出现过', to.path, beforeRouter);
    } else {
      // 点击过的路由，就删除这个路由后面的所有路由
      let index = beforeRouter.findIndex((v) => v.path == to.path)
      beforeRouter.splice(index + 1)
      // console.log('点击过的路由，就删除这个路由后面的所有路由', index, beforeRouter);

    }
  }
  let someI = beforeRouter.some((v) => {
    return v.path == '/index'
  })
  if (!someI) {
    beforeRouter = [
      {
        name: "首页",
        path: "/index"
      },
      ...beforeRouter
    ];
  }

  console.log('结果1', beforeRouter, to.path);
  store.commit("setBreadcrumbList", beforeRouter);
  store.commit("setActiveMenuIndex", to.path);
  next();
  if (to.query.title != null) {
    to.meta.title = to.query.title
  }
  // store.commit('setTreeName', '')
  next()
});
export default router
